package com.rentalcar.rental_car.controller;

import com.rentalcar.rental_car.entity.Customer;
import com.rentalcar.rental_car.entity.Result;
import com.rentalcar.rental_car.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @PostMapping("/customerList")
    public Result getList(){
        try {
            log.info("查询所以客户");
            return Result.success(customerService.getCustomerAll());
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/selectCname")
    public Result selectCname(@RequestBody Customer customer){
        try {
            log.info("查询" + customer.getCname() + "的信息");
            return Result.success(customerService.selectByCname(customer));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/selectIdCard")
    public Result selectIdCard(@RequestBody Customer customer){
        try {
            log.info("查找idCard为：" + customer.getIdCard() + "的客户");
            return Result.success(customerService.selectByIdCard(customer.getIdCard()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/updateCustomer")
    public Result update(@RequestBody Customer customer){
        try {
            log.info(customer.getCname() + "客户更新了信息");
            return Result.success(customerService.update(customer));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/loginCheckCustomer")
    public Result loginCheck(@RequestBody Customer customer){
        try {
            log.info(customer.getCname() + "客户正在登录");
            Result result = customerService.loginCheck(customer);
            if (result.getCode() == 1) {
                return Result.success(result.getData());
            } else if (result.getMsg().equals("账号密码错错误")) {
                return result;
            }
            return Result.fail("登录失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("登录失败");
    }
    @PostMapping("/register")
    public Result register(@RequestBody Customer customer){
        try {
            log.info("新客户正在注册");
            return customerService.addCustomer(customer);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/logOutCustomer")
    public Result logOut(@RequestBody Customer customer){
        try {
            log.info(customer.getCname() + "客户正在退出");
            return customerService.logOut(customer);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
}
